package cn.com.taurus.util;

import cn.com.taurus.biz.DiaryBiz;
import cn.com.taurus.controller.major.AdminsWebController;
import cn.com.taurus.entity.Diary;
import cn.com.taurus.service.DiaryService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName LogAsPect
 * @Description TODO
 * @Author Admin
 * @Date 2021/7/27 15:23
 * @Version 1.0
 */
@Aspect
@Component
@Controller
public class LogAspect {
    /**我这里是使用log4j2把一些信息打印在控制台上面，可以不写 */
    private static final Logger log = LogManager.getLogger(LogAspect.class);

    /**操作数据库 */
    @Autowired
    private DiaryBiz diaryBiz;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation(cn.com.taurus.util.Log)")
    public void logPointcut() {
    }

    //切面 配置通知
    @After("logPointcut()")         //AfterReturning
    public void saveOperation(JoinPoint joinPoint) {
        log.info("---------------接口日志记录---------------");
        //用于保存日志
        Diary diary = new Diary();
        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
        //获取操作--方法上的Log的值
        Log myLog = method.getAnnotation(Log.class);
        if (myLog != null) {
            //保存操作事件
            String operation = myLog.operation();
            diary.setOperation(operation);
            //保存日志类型
            int type = myLog.type();
            diary.setType(type);
            log.info("operation="+operation+",type="+type);
        }
        //拿到request作用域
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //拿到当时对应的地址
        String requestURL = request.getRequestURL().toString();
        //将地址放入日志对象
        diary.setUrl(requestURL);
        // 客户端ip
        String ip = request.getRemoteAddr();
        diary.setLoginIp(ip);
        AdminsWebController admins = new AdminsWebController();
        diary.setaId(admins.aId);
        diary.setaName(admins.aName);
        log.info("url="+requestURL,"ip="+ip);
        //调用service保存Operation实体类到数据库
        Date date=new Date();
        System.out.println("当前的日期是------>"+date);
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        diary.setCreateTime(format.format(date));

        //将日志存入数据库
        diaryBiz.insertLog(diary);
    }
}
